package com.sharing.service;

import com.sharing.backgroundmodule.service.UserStatisticsService;
import com.sharing.common.Result;
import com.sharing.domain.UserLogin;
import com.sharing.mapper.UserLoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MingTian
 * @version 1.0
 * @description TODO
 * @date 2023/6/22 23:58
 */
@Service
public class UserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private UserStatisticsService userStatisticsService;

    /**
     * 查询在指定时间范围内用户的登陆数
     * @param duration 指定时间范围
     * @return
     */
    public Result<List<Long>> selectUserLoginNumber(Integer duration) {
        return userStatisticsService.selectUserNumber(duration);
    }

    /**
     * 新增用户登录信息的业务层方法
     * @param userLogin
     * @return
     */
    public Result<Integer> insertUserLogin(UserLogin userLogin) {
        // 校验参数
        if (userLogin == null || userLogin.getUserId() == null) {
            return new Result<>(1, "参数不完整");
        }

        // 新增用户登录数据
        Integer row = userLoginMapper.insertUserLogin(userLogin);

        if (row == 0) {
            return new Result<>(1, "新增失败");
        }

        return new Result<>(0, row);
    }

    /**
     * 删除用户登录信息的业务层方法
     * @param id
     * @return
     */
    public Result<Integer> deleteUserLogin(Integer id) {
        // 校验参数
        if (id == null) {
            return new Result<>(1, "参数不能为空");
        }

        // 从数据库中删除
        Integer row = userLoginMapper.deleteUserLogin(id);

        if (row == 0) {
            return new Result<>(1, "删除失败");
        }

        return new Result<>(0, row);
    }

    /**
     * 查询某日登录的用户id
     * @param format
     * @return
     */
    public List<Integer> selectUserIdsByDay(String format) {
        List<Integer> ids = userLoginMapper.selectUserIdsByDay(format);

        return ids;
    }
}
